package com.example.ktpspringboot.service.impl;

import com.example.ktpspringboot.entity.Student;
import com.example.ktpspringboot.entity.User;
import com.example.ktpspringboot.mapper.StudentMapper;
import com.example.ktpspringboot.service.LoginService;
import com.example.ktpspringboot.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    String judgeAccount = "^ktp\\d{10}$";
    String judgePhone = "^\\d{11}$";
    String judgeEmail = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$";

    @Autowired
    private StudentMapper studentMapper;
    @Override
    public String checkLogin(User user) {
        if (dataVerification(user.getUsername(), judgeAccount)) {
            Student student = studentMapper.queryByAccount(user.getUsername());
            return checkPassword(student, user);
        } else if (dataVerification(user.getUsername(), judgeEmail)) {
            Student student = studentMapper.queryByEmail(user.getUsername());
            return checkPassword(student, user);
        } else if (dataVerification(user.getUsername(), judgePhone)) {
            Student student = studentMapper.queryByPhone(user.getUsername());
            return checkPassword(student, user);
        } else {
            return "The user name type is incorrect";
        }
    }

    @Override
    public boolean dataVerification(String arg, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(arg);
        boolean matchesId = matcher.matches();
        if (arg.equals("")) {
            return false;
        }
        return matchesId;
    }

    @Override
    public String checkPassword(Student student, User user) {
        if (student != null) {
            if (Objects.equals(student.getPassword(), user.getPassword())) {
                Map<String,Object> map = new HashMap<>();
                try{
                    Map<String,String> payload = new HashMap<>();
                    payload.put("username",user.getUsername());
                    payload.put("password",user.getPassword());
                    //生成JWT令牌
                    String token = JwtUtil.getToken(payload);
                    map.put("state",true);
                    map.put("msg","认证成功");
                    map.put("token",token);//响应token
                    return "{\"role\": \"" + student.getRole() + "\", \"token\": \"" + token + "\"}";
                } catch (Exception e) {
                    map.put("state","false");
                    map.put("msg",e.getMessage());
                }
            } else {
                return "password error";
            }
        } else {
            return "user does not exist";
        }
        return null;
    }
}
